package com.gdut.simple.view.basic;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.ProgressUtils;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UpdateThemeEvent;

import cn.bmob.v3.BmobUser;
import rx.Subscription;
import rx.functions.Action1;

public abstract class AbsBasicActivity extends AppCompatActivity {

    protected Subscription mSubscription;
    private BasicApplication mApplication;
    private WindowManager mWindowManager;
    private View mNightView = null;
    private boolean isAddedView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mApplication = (BasicApplication) getApplication();
        if (mApplication.isNightMode()) {
            setTheme(R.style.AppTheme_night);
        } else {
            setTheme(R.style.AppTheme_day);
        }
        super.onCreate(savedInstanceState);
        if (mApplication.isNightMode()) {
            initNightView();
            mNightView.setBackgroundResource(R.color.night_mask);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setElevation(0);
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_LAYOUT_FLAGS | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
            getWindow().getDecorView().setFitsSystemWindows(true);
        }
        initView(savedInstanceState);
        loadData();
        RxBus.getDefault().toObservable(UpdateThemeEvent.class).subscribe(new Action1<UpdateThemeEvent>() {
            @Override
            public void call(UpdateThemeEvent updateThemeEvent) {
                recreateOnResume();
            }
        });
    }

    private void initNightView() {
        if (isAddedView) return;
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        mNightView = new View(this);
        mWindowManager.addView(mNightView, params);
        isAddedView = true;
    }

    protected void changeToDayMode() {
        mApplication.setNightMode(false);
        mNightView.setBackgroundResource(R.color.day_mask);
    }

    protected void changeToNightMode() {
        mApplication.setNightMode(true);
        initNightView();
        mNightView.setBackgroundResource(R.color.night_mask);
    }

    public void recreateOnResume() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                recreate();
            }
        }, 10);
    }

    @Override
    protected void onDestroy() {
        if (isAddedView) {
            mApplication = null;
            mWindowManager.removeViewImmediate(mNightView);
            mWindowManager = null;
            mNightView = null;
        }
        super.onDestroy();
        if (mSubscription != null && mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }

    protected abstract void initView(Bundle savedInstanceState);

    protected void loadData() {

    }

    public void showMessage(String message) {
        ToastUtils.showShort(getApplicationContext(), message);
    }

    public boolean isLogin() {
        return BmobUser.getCurrentUser(User.class) != null
                && BmobUser.getCurrentUser(User.class).getObjectId() != null;
    }

    public void showProgress(String message) {
        ProgressUtils.show(this, message);
    }

    public void hideProgress() {
        ProgressUtils.hide();
    }
}
